package com.aube.plugin.gallery.preview;

import com.aube.plugin.gallery.Comment;
import com.huyn.baseframework.utils.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * Created by huyaonan on 16/9/20.
 */
public class FavorManager {

    private static FavorManager mInstance;

    private HashMap<String, Boolean> mFavorMaps;
    private HashMap<String, Integer> mFavorCountMaps;
    private HashMap<String, Integer> mCommentCountMaps;

    private FavorManager() {
        initFavorMaps();
    }

    private void initFavorMaps() {
        if(mFavorMaps == null)
            mFavorMaps = new HashMap<>();
        if(mFavorCountMaps == null)
            mFavorCountMaps = new HashMap<>();
        if(mCommentCountMaps == null)
            mCommentCountMaps = new HashMap<>();
    }

    public static synchronized FavorManager getInstance() {
        if(mInstance == null)
            mInstance = new FavorManager();
        return mInstance;
    }

    public void update(String id, boolean state) {
        if(StringUtils.isBlank(id))
            return;
        initFavorMaps();
        mFavorMaps.put(id, state);
    }

    public void update(List<Comment> list) {
        if(list != null) {
            initFavorMaps();
            for(Comment comment : list) {
                mFavorMaps.put(comment.commentId, comment.isPraised());
                mFavorCountMaps.put(comment.commentId, comment.praiseCount);
            }
        }
    }

    public void addFavorCount(String id, int count) {
        if(StringUtils.isBlank(id))
            return;
        initFavorMaps();
        mFavorCountMaps.put(id, count < 0 ? 0 : count);
    }

    public int getFavorCount(String id) {
        if(mFavorCountMaps == null || !mFavorCountMaps.containsKey(id))
            return 0;
        return mFavorCountMaps.get(id);
    }

    public void addCommentCount(String id, int count) {
        if(StringUtils.isBlank(id))
            return;
        initFavorMaps();
        mCommentCountMaps.put(id, count);
    }

    public int getCommentCount(String id) {
        if(mCommentCountMaps == null || !mCommentCountMaps.containsKey(id))
            return 0;
        return mCommentCountMaps.get(id);
    }

    public boolean isFavored(String id) {
        if(mFavorMaps != null && mFavorMaps.containsKey(id))
            return mFavorMaps.get(id);
        return false;
    }

}
